<template>
  <van-button
    class="follow-btn"
    type="info"
    v-if="value"
    color="#3296fa"
    round
    size="small"
    icon="plus"
    @click="onFollow"
    :loading="loading"
    >关注</van-button
  >
  <van-button
    v-else
    class="follow-btn"
    round
    size="small"
    @click="onFollow"
    :loading="loading"
    >已关注</van-button
  >
</template>

<script>
import { followUser, cancelFollow } from '@/api/user';

export default {
  name: 'FollowUser',
  props: {
    value: {
      typeof: Boolean,
      require: true
    },
    userId: {
      typeof: [Number, String, Object],
      require: true
    }
  },
  data () {
    return {
      loading: false
    };
  },

  mounted () {},

  methods: {
    async onFollow () {
      this.loading = true;
      try {
        if (this.value) {
          // console.log(this.article.aut_id);
          await cancelFollow(this.userId);
          // this.article.aut_id = false;
        } else {
          // console.log(this.article.aut_id);
          await followUser(this.userId);
        }
        // this.isFollowed = !isFollowed;
        this.$emit('input', !this.value);
      } catch (error) {
        console.log(error);
        let message = '操作失败，请稍后重试';
        if (error.response && error.response.status === 400) {
          message = '用户不能关注自己!';
        }
        this.$toast(message);
      }
      this.loading = false;
    }
  }
};
</script>

<style lang="less" scoped>
</style>
